package Utils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class DaoUtils {

	private static String jdbcUrl = "jdbc:mysql://localhost:3306/cuit_asset_management_system?useUnicode=true&characterEncoding=utf-8&useSSL=false";
	private static String userName = "root";
	private static String pwd = "123456";
	
	static {

		try {
			//Class.forName(com.mysql.jdbc.Driver)
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		
	}
	
	public DaoUtils() {
		
	}

	public static Connection getConnection() {
		Connection connection = null;
		
		try {
			connection = DriverManager.getConnection(jdbcUrl,userName,pwd);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return connection;
	}
	
	public static PreparedStatement getPreparedStatement(Connection connection,String sql) {
		PreparedStatement preparedStatement = null;
		try {
			//Statement statement = connection.createStatement();
			if(connection == null)
				System.out.println("hhhhhh");
			preparedStatement = connection.prepareStatement(sql);
			
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return preparedStatement;
	} 
	
	public static void close(Connection connection , PreparedStatement preparedStatement) {
		if(connection != null)
			try {
				connection.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		if(preparedStatement != null)
			try {
				preparedStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
	
}
